<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${Data.daoInterfase}">
	<!-- 定义${Data.tableClassModel.className} 的复杂关联map -->
	<resultMap type="${Data.tableClassModel.className}" id="${Data.tableClassModel.className}Map">
		<#list Data.tableClassModel.mapping as item>
			<#if item.isPrimaryKey>
			<id property="${item.property}" column="${item.column}" />
			<#else>
			<result property="${item.property}" column="${item.column}" />
			</#if>
		</#list>	
	</resultMap>
	
	
	<!-- 定义${Data.tableClassModel.className} 的简单map  ，本map不添加其他的关联属性 -->
	<resultMap type="${Data.tableClassModel.className}" id="${Data.tableClassModel.className}SimpleMap">
		<#list Data.tableClassModel.mapping as item>
			<#if item.isPrimaryKey>
			<id property="${item.property}" column="${item.column}" />
			<#else>
			<result property="${item.property}" column="${item.column}" />
			</#if>
		</#list>	
	</resultMap>
	
	
	
	<!--  插入方法   -->
	<insert id="insert" parameterType="${Data.tableClassModel.className}"
		useGeneratedKeys="true" keyProperty="${Data.tableClassModel.primaryKey.property}">
		INSERT INTO ${Data.tableClassModel.tableName} (
		<#list Data.tableClassModel.mapping as item> 
			<#if item_has_next>
			${item.column},
			<#else>
			${item.column}
			</#if>
		</#list>	
		)
	VALUES (
		<#list Data.tableClassModel.mapping as item>
		<#if item_has_next>
			${'#'}{${item.property}},
			<#else>
			${'#'}{${item.property}}
			</#if>
		</#list>	
	)
	</insert>
	
	
	<!--  根据id更新 部分更新   -->
	<update id="update" parameterType="${Data.tableClassModel.primaryKey.classType}">
		UPDATE ${Data.tableClassModel.tableName}
		<set>
			<#list Data.tableClassModel.mapping as item>
			<#if !item.isPrimaryKey>
				<if test="(${item.property}!=null and ${item.property}!='') or (${item.property}!='' and ${item.property}==0)">
					${item.column} = ${'#'}{${item.property}},
				</if>		
			</#if>
			</#list>
		</set>
		WHERE ${Data.tableClassModel.primaryKey.column}=${'#'}{${Data.tableClassModel.primaryKey.property}} 
	</update>
	
	
	
	<!-- 批量删除 -->
	<delete id="deleteByIds" parameterType="java.util.List">
		delete from ${Data.tableClassModel.tableName} where  ${Data.tableClassModel.primaryKey.column} in
		<foreach collection="list" index="index" item="item" open="("
			separator="," close=")">
			${'#'}{item}
		</foreach>
	</delete>
		
	<!-- 根据id删除-->
	<delete id="deleteById" parameterType="${Data.tableClassModel.primaryKey.classType}">
		DELETE FROM ${Data.tableClassModel.tableName}
		where  ${Data.tableClassModel.primaryKey.column}=${'#'}{${Data.tableClassModel.primaryKey.property}} 
	</delete>
	
	<!-- 根据对象删除-->
	<delete id="deleteByModel" parameterType="${Data.tableClassModel.primaryKey.classType}">
		DELETE FROM ${Data.tableClassModel.tableName}
		where 1=1
		<if test="record!=null">
			<#list Data.tableClassModel.mapping as item> 
			<if test="(record.${item.property}!=null and record.${item.property}!='') or (record.${item.property}!='' and record.${item.property}==0)">
				and ${item.column} = ${'#'}{record.${item.property}} 
			</if>
			</#list>	
		</if>
	</delete>
	
	
	
	<!-- 分页查询 -->
	<select id="selectInPage" resultMap="${Data.tableClassModel.className}Map">
		select 
		<#list Data.tableClassModel.mapping as item> 
			<#if item_has_next>
			${item.column},
			<#else>
			${item.column}
			</#if>
		</#list>	
		from ${Data.tableClassModel.tableName}
		where 1=1
		<if test="record!=null">
			<#list Data.tableClassModel.mapping as item> 
			<if test="(record.${item.property}!=null and record.${item.property}!='') or (record.${item.property}!='' and record.${item.property}==0)">
				and ${item.column}  = ${'#'}{record.${item.property}} 
			</if>
			</#list>	
		</if>
		<if test="pageVo !=null"><!-- 判断pageVo对象是否为空 -->
			<if test="pageVo.sort !=null  and pageVo.order !=null">
				order by
				${'$'}{pageVo.sort} ${'$'}{pageVo.order}
			</if>
			<if test="pageVo.offset >=0  and pageVo.limit >0">
				limit
				${'#'}{pageVo.offset},${'#'}{pageVo.limit}
			</if>
		</if>
	</select>
	
	<!-- 查询总条数 -->
	<select id="selectTotalRecord" parameterType="long"   resultType="java.lang.Integer">
	select count(*)
	from ${Data.tableClassModel.tableName}
		where 1=1
		<if test="record!=null">
			<#list Data.tableClassModel.mapping as item> 
			<if test="(record.${item.property}!=null and record.${item.property}!='') or (record.${item.property}!='' and record.${item.property}==0)">
			and ${item.column} = ${'#'}{record.${item.property}} 
			</if>
			</#list>	
		</if>
	</select>

	<!-- 根据id查询-->
	<select id="selectById" resultMap="${Data.tableClassModel.className}Map">
		select 
		<#list Data.tableClassModel.mapping as item> 
			<#if item_has_next>
			${item.column},
			<#else>
			${item.column}
			</#if>
		</#list>	
		from ${Data.tableClassModel.tableName}
		where  ${Data.tableClassModel.primaryKey.column}=${'#'}{${Data.tableClassModel.primaryKey.property}} 
	</select>	
	
	
	<!-- 根据对象查询-->
	<select id="selectByModel" resultMap="${Data.tableClassModel.className}Map">
		select 
		<#list Data.tableClassModel.mapping as item> 
			<#if item_has_next>
			${item.column},
			<#else>
			${item.column}
			</#if>
		</#list>	
		from ${Data.tableClassModel.tableName}
		where 1=1
		<if test="record!=null">
			<#list Data.tableClassModel.mapping as item> 
			<if test="(record.${item.property}!=null and record.${item.property}!='') or (record.${item.property}!='' and record.${item.property}==0)">
				and ${item.column} = ${'#'}{record.${item.property}} 
			</if>
			</#list>	
		</if>
	</select>
</mapper>